package tenantchannellogic

import (
	"context"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/tenant/channel"
	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type WavepayUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewWavepayUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WavepayUpdateLogic {
	return &WavepayUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *WavepayUpdateLogic) WavepayUpdate(in *rpc.WavepayUpdateRequest) (*rpc.WavepayUpdateResponse, error) {
	var (
		CommonResp rpc.CommonResp
		wavepay    schema.TenantChannelWavepay
		err        error
	)

	wavepayModel := model.NewTenantChannelWavepayModel(l.ctx, l.svcCtx.Db)
	if in.Code != "" {
		wavepay.Code = in.Code
	}
	if in.Phone != "" {
		wavepay.Phone = in.Phone
	}
	if in.CardHolder != "" {
		wavepay.CardHolder = in.CardHolder
	}
	if in.MinAmount != 0 {
		wavepay.MinAmount = in.MinAmount
	}
	if in.MaxAmount != 0 {
		wavepay.MaxAmount = in.MaxAmount
	}
	if in.DailyTimes != 0 {
		wavepay.DailyTimes = in.DailyTimes
	}
	if in.DailyAmount != 0 {
		wavepay.DailyAmount = in.DailyAmount
	}
	if in.RemainTimes != 0 {
		wavepay.RemainTimes = in.RemainTimes
	}
	if in.RemainAmount != 0 {
		wavepay.RemainAmount = in.RemainAmount
	}
	if in.VisionLevel != "" {
		wavepay.VisionLevel = in.VisionLevel
	}
	if in.IsDisplay != 0 {
		wavepay.IsDisplay = in.IsDisplay
	}
	wavepay.ID = in.Id
	err = wavepayModel.UpdateWavepay(&wavepay)
	if err != nil {
		CommonResp.Code = xerrors.DBError
		CommonResp.Message = "Database operation error"
		return &rpc.WavepayUpdateResponse{
			CommonResp: &CommonResp,
		}, err
	}

	CommonResp.Message = "Success"
	return &rpc.WavepayUpdateResponse{
		CommonResp: &CommonResp,
	}, nil
}
